using Microsoft.Win32;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace Glpt.Common
{
    /// <summary>
    /// 访问URL类
    /// </summary>
    public class OpenURL
    {
        private string filename = "";
        private string url = "";
        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        public OpenURL()
        {
            RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command\");
            string s = key.GetValue("").ToString();

            Regex reg = new Regex("\"([^\"]+)\"");
            MatchCollection matchs = reg.Matches(s);

            if (matchs.Count > 0)
            {
                filename = matchs[0].Groups[1].Value;
            }
        }

        /// <summary>
        /// 设置URL参数
        /// </summary>
        /// <param name="url">URL</param>
        public void SetParams(string url)
        {
            SetParams(url, "");
        }

        /// <summary>
        /// 设置URL参数
        /// </summary>
        /// <param name="url1">URL</param>
        /// <param name="url2">URL</param>
        public void SetParams(string url1, string url2)
        {
            SetParams(url1, url2, "");
        }

        /// <summary>
        /// 设置URL参数
        /// </summary>
        /// <param name="url1">URL</param>
        /// <param name="url2">URL</param>
        /// <param name="name">NAME</param>
        public void SetParams(string url1, string url2, string name)
        {
            this.url = url1 + url2;
        }

        /// <summary>
        /// 显示界面
        /// </summary>
        public System.Windows.Forms.DialogResult ShowDialog()
        {
            Show();
            return System.Windows.Forms.DialogResult.None;
        }

        /// <summary>
        /// 显示界面
        /// </summary>
        public void Show()
        {
            if (filename.Length > 0 && url.Length > 0)
                System.Diagnostics.Process.Start(filename, url);
        }

        private static WebClient GetWebClient(string host, int port)
        {
            WebClient wc = new WebClient();
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
            wc.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)");
            if (host.ToUpper().Equals("NO"))
                wc.Proxy = null;
            else if (host.Length > 3)
                wc.Proxy = new WebProxy(host, port > 0 ? port : 1080);
            return wc;
        }
        /// <summary>
        /// URL下载文件
        /// </summary>
        /// <param name="url">URL</param>
        /// <param name="destpath">保存的路径</param>
        /// <param name="host">代理IP</param>
        /// <param name="port">代理端口号</param>
        public static void DownloadFile(string url, string destpath, string host, int port)
        {
            WebClient wc = GetWebClient(host, port);
            FileInfo sfile = new FileInfo(destpath);
            if (sfile.Exists) sfile.Delete();
            if (!sfile.Directory.Exists) sfile.Directory.Create();
            wc.DownloadFile(url, destpath);
        }
        /// <summary>
        /// 获取URL数据流
        /// </summary>
        /// <param name="url">URL</param>
        /// <param name="host">代理IP</param>
        /// <param name="port">代理端口号</param>
        /// <returns></returns>
        public static Stream DownloadStream(string url, string host, int port)
        {
            WebClient wc = GetWebClient(host, port);
            return wc.OpenRead(url);
        }
        /// <summary>
        /// 获取URL字符串
        /// </summary>
        /// <param name="url">URL</param>
        /// <param name="encode">编码</param>
        /// <param name="host">代理IP</param>
        /// <param name="port">代理端口号</param>
        /// <returns></returns>
        public static string DownloadString(string url, Encoding encode, string host, int port)
        {
            WebClient wc = GetWebClient(host, port);
            wc.Encoding = encode;
            return wc.DownloadString(url);
        }
        /// <summary>
        /// 获取URL字符串
        /// </summary>
        /// <param name="url">URL</param>
        /// <param name="post">post参数</param>
        /// <param name="header">header参数</param>
        /// <param name="encode">编码</param>
        /// <param name="host">代理IP</param>
        /// <param name="port">代理端口号</param>
        /// <returns></returns>
        public static string DownloadString(string url, string post, Dictionary<string, string> header, Encoding encode, string host, int port)
        {
            WebClient wc = GetWebClient(host, port);
            wc.Encoding = encode;
            if (header != null)
            {
                foreach (string key in header.Keys)
                {
                    wc.Headers.Add(key, header[key]);
                }
            }
            if (post.Length == 0)
                return wc.DownloadString(url);
            else
                return wc.UploadString(url, post);
        }
    }
}
